package com.woniu.rabbit.routing_model.exchange_queue_conf;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 路由模式配置
 * */
@Configuration
@Slf4j
public class RouteExchangeAndQueueConfig {
    /**
     * 定义名称
     * */
    public static final String EXCHANGE_NAME = "routing.exchange";
    public static final String QUEUE_NAME01 = "routing.queue01";
    public static final String QUEUE_NAME02 = "routing.queue02";

    /**
     * 定义路由对象
     * */
    @Bean
    public DirectExchange routeExchange(){
        return new DirectExchange(EXCHANGE_NAME);
    }

    /**
     * 定义 消息队列
     * */
    @Bean
    public Queue routeQueue01(){
        return new Queue(QUEUE_NAME01);
    }

    @Bean
    public Queue routeQueue02(){
        return new Queue(QUEUE_NAME02);
    }

    /**
     * 定向绑定
     * queue1 绑定到交换机并监听路由键 a。
     * queue2 绑定到交换机并监听路由键 a、b、c。
     * */
    @Bean
    public Binding routeBinding01A(DirectExchange routeExchange, Queue routeQueue01){
        return BindingBuilder.bind(routeQueue01)
                .to(routeExchange)
                .with("a");
    }

    @Bean
    public Binding routeBinding02A(DirectExchange routeExchange, Queue routeQueue02){
        return BindingBuilder.bind(routeQueue02)
                .to(routeExchange)
                .with("a");
    }

    @Bean
    public Binding routeBinding02B(DirectExchange routeExchange, Queue routeQueue02){
        return BindingBuilder.bind(routeQueue02)
                .to(routeExchange)
                .with("b");
    }

    @Bean
    public Binding routeBinding02C(DirectExchange routeExchange, Queue routeQueue02){
        return BindingBuilder.bind(routeQueue02)
                .to(routeExchange)
                .with("c");
    }
}
